package com.qianseit.westore.activity.account;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianseit.westore.BaseDoFragment;
import com.qianseit.westore.Run;
import com.qianseit.westore.activity.AgentActivity;
import com.qianseit.westore.http.JsonRequestBean;
import com.qianseit.westore.http.JsonRequestBean.JsonRequestCallback;
import com.qianseit.westore.http.JsonTask;
import com.qianseit.westore.http.JsonTaskHandler;
import com.beautymiracle.androidclient.R;

public class MyScoreFragment extends BaseDoFragment {

	private LinearLayout mContentLayout;
	private List<String> String;
	private String mOnlineScore;
	private String mOfflineScore;
	private String mResponse;
	private MyScoreAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActionBar.setTitle(R.string.myscore);
	}

	@Override
	public void init(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_myscore, null);
		mContentLayout = (LinearLayout) rootView
				.findViewById(R.id.fragment_myscore_content);
		new JsonTask().execute(new GetOnlineScore());
		new JsonTask().execute(new GetOfflineScore());
	}

	private class GetOnlineScore implements JsonTaskHandler {

		@Override
		public void task_response(String json_str) {
			hideLoadingDialog();
			mResponse = json_str;
			try {
				JSONObject all = new JSONObject(json_str);
				if (Run.checkRequestJson(mActivity, all) && !isDetached()) {
					JSONObject data = all.optJSONObject("data");
					mOnlineScore = data.optString("total");
					mAdapter = new MyScoreAdapter();
					mContentLayout.addView(mAdapter.getView(0, null, null));
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public JsonRequestBean task_request() {
			showLoadingDialog();
			JsonRequestBean req = new JsonRequestBean(Run.API_URL,
					"mobileapi.point.point_detail");
			req.addParams("n_page", "1");
			return req;
		}

	}

	private class GetOfflineScore implements JsonTaskHandler {

		@Override
		public void task_response(java.lang.String json_str) {
			try {
				JSONObject all = new JSONObject(json_str);
				if (Run.checkRequestJson(mActivity, all) && !isDetached()) {
					JSONObject obj = all.optJSONObject("data");
					if (obj != null) {
						mOfflineScore = obj.optString("IntegralBalance");
						mContentLayout.addView(mAdapter.getView(1, null, null));
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public JsonRequestBean task_request() {
			JsonRequestBean req = new JsonRequestBean(Run.API_URL,
					"mobileapi.point.offline_point");
			return req;
		}
	}

	private class MyScoreAdapter extends BaseAdapter implements
			View.OnClickListener {

		@Override
		public int getCount() {
			return 2;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(mActivity).inflate(
						R.layout.item_myscore, null);
				convertView.setOnClickListener(this);
			}
			if (position == 0) {
				convertView.findViewById(R.id.item_myscore_arrow);
			} else {
				convertView.findViewById(R.id.item_myscore_arrow)
						.setVisibility(View.INVISIBLE);
			}
			if (TextUtils.isEmpty(mOnlineScore)) {
				mOnlineScore = "0";
			}
			if (position == 0) {
				((TextView) convertView.findViewById(R.id.item_myscore_score))
						.setText(mOnlineScore);
				((TextView) convertView.findViewById(R.id.item_myscore_title))
						.setText(getString(R.string.myscore_online));
				convertView.setTag(getString(R.string.myscore_online));
			} else {
				// 暂时只有线上积分
				((TextView) convertView.findViewById(R.id.item_myscore_score))
						.setText(mOfflineScore);
				((TextView) convertView.findViewById(R.id.item_myscore_title))
						.setText(getString(R.string.myscore_offline));
				convertView.setTag(getString(R.string.myscore_offline));
			}
			return convertView;
		}

		@Override
		public void onClick(View v) {
			String tag = (java.lang.String) v.getTag();
			if (!tag.equals(getString(R.string.myscore_online))) {
				return;
			}
			startActivity(AgentActivity.intentForFragment(mActivity,
					AgentActivity.FRAGMENT_MYSCORE_DESTAIL).putExtra(
					Run.EXTRA_DATA, mResponse));
		}

	}
}
